
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

package com.hlkj.pay.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.common.constants.MqConstant;
import com.hlkj.pay.config.ExtRocketMQTemplate;
import com.hlkj.pay.dto.LocalMerchantRequest;
import com.hlkj.pay.infrastructure.model.common.RequestLogDO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @data 2022/2/8 13:22
 */
@Component
@Slf4j
@Order(Integer.MIN_VALUE)
public class CallBackApiInterceptor implements HandlerInterceptor {

    @Autowired
    private ExtRocketMQTemplate extRocketMQTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
        if (StringUtils.isBlank(servletPath)) {
            servletPath = request.getPathInfo();
        }
        log.info("callBackApiInterceptor:{}", servletPath);
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((key, value) -> {
            log.info("key:{}", key);
            try {
                log.info("value:{}", JsonUtils.toJsonString(value));
            }
            catch (Exception e) {
                log.error("error:", e);
            }
        });
        LocalMerchantRequest localMerchantRequest = new LocalMerchantRequest();
        localMerchantRequest.setStartTime(System.currentTimeMillis());
        localMerchantRequest.setRequestUrl(servletPath);
        localMerchantRequest.setRequestMethod(request.getMethod());
        LocalContext.set(localMerchantRequest);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        LocalMerchantRequest localMerchantRequest = (LocalMerchantRequest) LocalContext.get();
        RequestLogDO requestLogDO = new RequestLogDO();
        requestLogDO.setCostTime(System.currentTimeMillis() - localMerchantRequest.getStartTime());
        requestLogDO.setAppId(localMerchantRequest.getAppId());
        requestLogDO.setSn(localMerchantRequest.getSn());
        requestLogDO.setSubSn(localMerchantRequest.getSubSn());
        requestLogDO.setChannelCode(localMerchantRequest.getChannelCode());
        requestLogDO.setProviderCode(localMerchantRequest.getProviderCode());
        requestLogDO.setMchChannelCode(localMerchantRequest.getMchChannelCode());
        requestLogDO.setRequestId(localMerchantRequest.getRequestId());
        requestLogDO.setRequestUrl(localMerchantRequest.getRequestMethod() + " " + localMerchantRequest.getRequestUrl());
        requestLogDO.setRequestParams(localMerchantRequest.getParams());
        // 获取请求参数
        requestLogDO.setResponse(localMerchantRequest.getResponse());
        requestLogDO.setIp(localMerchantRequest.getIp());
        requestLogDO.setIpLocation(localMerchantRequest.getIpLocation());
        requestLogDO.setCreateTime(System.currentTimeMillis());
        try {
            extRocketMQTemplate.send(MqConstant.REQUEST_LOG_TOPIC, requestLogDO);
        }
        catch (Exception e) {
            log.error("请求参数记录失败:{}", e.getMessage());
        }
        LocalContext.destroy();
    }

}
